package com.yanqu.road.dao.impl.match;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.match.RankRemoveData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/3/18 22:45
 */
public class RankRemoveDataDaoImpl extends TempDataObjectDao<RankRemoveData> {

    public static String addSql = "INSERT INTO t_c_rank_remove_data(weekId, serverId, userId, userBaseInfo, value) VALUES(?,?,?,?,?)";
    @Override
    protected boolean addData(RankRemoveData data) {
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_c_rank_remove_data SET userBaseInfo = ?, value = ?  WHERE weekId = ? AND serverId = ? AND userId = ?";
    @Override
    protected boolean updateData(RankRemoveData data) {
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected RankRemoveData parseData(ResultSet rs) throws SQLException {
        RankRemoveData data = new RankRemoveData();
        data.setWeekId(rs.getInt("weekId"));
        data.setServerId(rs.getLong("serverId"));
        data.setUserId(rs.getLong("userId"));
        String userBaseInfoStr = rs.getString("userBaseInfo");
        if (!StringUtils.isNullOrEmpty(userBaseInfoStr)) {
            data.setUserBaseInfo(JSON.parseObject(userBaseInfoStr, new TypeReference<UserBaseInfo>() {}));
        }
        data.setValue(rs.getLong("value"));
        return data;
    }

    public static String selectSql = "SELECT * FROM t_c_rank_remove_data WHERE weekId = ?";
    public Map<Integer, Map<Long, Map<Long, RankRemoveData>>> getRankRemoveDataMap(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Long, Map<Long, RankRemoveData>>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                RankRemoveData data = parse(rs);
                if (!map.containsKey(data.getWeekId())) {
                    map.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                if (!map.get(data.getWeekId()).containsKey(data.getServerId())) {
                    map.get(data.getWeekId()).put(data.getServerId(), new ConcurrentHashMap<>());
                }
                map.get(data.getWeekId()).get(data.getServerId()).put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
